/*
 * adaboost_facedetect_old.h
 *
 *  Created on: 2016年3月15日
 *      Author: guyadong
 */

#ifndef FACEDETECT_ADABOOST_FACEDETECT_OLD_H_
#define FACEDETECT_ADABOOST_FACEDETECT_OLD_H_
#include "CImgWrapper.h"
#include "file_utilits.h"
namespace gdface{
#define MAXVAR 93.6//106
#define MINVAR 13.1
#define SCALE 1.25
#define StartScaleSize 0
#define ScaleSize 10

#define FEATHER_NUM 15960
#define FACEINFO_SIZE (512*1024)
typedef struct {int rows; int cols; unsigned char* data;} sImage;

typedef struct { int lui; int luj; int h; int w; } AREA;  // insert 00.11.3

typedef struct{
			int upx;
			int upy;
			int width;
			int height;

//Mask 00.12.7  unsigned char *faceImage; // insert 00.11.3

            AREA leftEyeArea;   // insert 00.11.3
            AREA rightEyeArea;  // insert 00.11.3
            AREA leftPupil;     // insert 00.11.3
            AREA rightPupil;    // insert 00.11.3
            AREA noseArea;      // insert 00.11.3
			AREA mouthArea;     // insert 00.11.3

          } FACE_LOCATION;


typedef struct{
	int UpX;
	int UpY;
	int DownX;
	int DownY;
	float Mean;
	float Variance;
}FACEINFO;
struct  Link{
	FACEINFO face;
	Link * next;
};

typedef struct{
	int x;//特征矩形所在位置
	int y;
	int Width;

	int Height;//特征矩阵大小
	int Up_Down;
	unsigned char RectangleNum;//矩形数目
	int  Valve;//阀值
	float DValve;
	int Sign;//阀值的方向
	int CorrectNum;
	int NonfaceCorrectNum;
	int unitWidth;
	int unitHeight;
}FEATHER;

typedef struct{
	int CasNum;//总层数
	int FeaNumPerLayer[50];//每层特征的数目
	double Weight[6000];//每个特征权值
	double LayerWeight[50];//每层权值
	FEATHER * Feather;//特征
	int FeatherNumPerCas;//特征总数目
}CASINFO;
class DETECTFACEINFO
{
public:
	uint8_t * pFaceInfo;
	std::vector<FACEINFO> hFaceInfo;
	FACEINFO * DetectedFace[2];
	int DetectedFaceNum[2];
	DETECTFACEINFO();
	virtual ~DETECTFACEINFO();

};


class CAdaboostFaceDetect
{
public:
	CAdaboostFaceDetect(std::istream&& in);
	CAdaboostFaceDetect(std::istream& in):CAdaboostFaceDetect(std::move(in)){}
	CAdaboostFaceDetect(const std::string& file);
	virtual ~CAdaboostFaceDetect()=default;
public:
	FACE_LOCATION face_info[1000];
	int m_nFaceNum=0;
	int m_nNumScanedWin=0;
	std::vector<FEATHER> hFeature;

public:

	CASINFO Cas;
	DETECTFACEINFO Face;
	bool RunFaceDetector(unsigned char * pImage, int Width, int Height, FACE_LOCATION * rface, int * FaceNumber);
	void IntegralE(unsigned char *Image, int *width, int *height, long *IntegralImage);
	void IntegralSqurE(unsigned char *Image, int *width, int *height, uint32_t *IntegralImage);
	void MagnifyFeather();
	template<typename T,typename ...Args>
	static void display_face_rect(const cimg_library::CImg<T> & src_img
			,const std::vector<FACE_LOCATION>& face_list
			,bool shared=false
			,Args&&...args){
		cimg_library::CImg<T> copy;
		cimg_library::CImg<T>& dst=shared?const_cast<cimg_library::CImg<T>&>(src_img):copy =src_img;

		uint8_t color[] = { 255,0,0 };
		uint8_t text_color[] = { 0,255,0 };
		for(size_t i=0;i<face_list.size();++i){
			auto face_info=face_list[i];
			std::stringstream stream;
			stream << face_info.upx << "," << face_info.upy<<"\nW"<< face_info.width<<"H"<< face_info.height;
			dst.draw_text(int(face_info.upx+1), int(face_info.upy+1), stream.str().data(), text_color);
			dst.draw_rectangle(int(face_info.upx),int(face_info.upy),int(face_info.upx+face_info.width),int(face_info.upy+face_info.height),color,0.9f, ~0U);
		}
		std::stringstream stream;
		args_print(stream,std::forward<Args>(args)...);
		stream<<" "<<face_list.size()<<" faces";
		dst.display(stream.str().data());
	}
};
}/* namespace gdface */



#endif /* FACEDETECT_ADABOOST_FACEDETECT_OLD_H_ */
